-- Tags: long

SET query_plan_optimize_join_order_limit = 0;
SET query_plan_join_swap_table = 0;
SET enable_analyzer = 1;
SET join_algorithm = 'hash';
SET enable_lazy_columns_replication = 0;

SELECT count(), sum(ignore(*))
FROM ( SELECT materialize(1) AS c0, materialize(repeat('a', 100_000)) as payload FROM numbers(2) ) AS t1
JOIN ( SELECT materialize(1) AS c0 FROM numbers(100_000) ) t2
ON t1.c0 = t2.c0
SETTINGS joined_block_split_single_row = 0, max_joined_block_size_rows = 100, max_memory_usage = '1G'; -- { serverError MEMORY_LIMIT_EXCEEDED }

SELECT count(), sum(ignore(*))
FROM ( SELECT materialize(1) AS c0, materialize(repeat('a', 100_000)) as payload FROM numbers(2) ) AS t1
JOIN ( SELECT materialize(1) AS c0 FROM numbers(100_000) ) t2
ON t1.c0 = t2.c0
SETTINGS joined_block_split_single_row = 1, max_joined_block_size_rows = 100, max_memory_usage = '1G';

SET joined_block_split_single_row = 1;

SELECT
    if(max(blockSize()) > 9, 'Error: ' || toString(max(blockSize())), 'Ok'), count() as total_rows, sum(ignore(*)) as dummy
FROM ( SELECT intDiv(number, 3) AS c0, 'a' || toString(number) as payload FROM numbers(10) ) AS t1
JOIN ( SELECT 1 AS c0, 'b' || toString(number) as payload FROM numbers(100) ) t2
ON t1.c0 = t2.c0
SETTINGS max_joined_block_size_rows = 9;

SELECT
    if(max(blockSize()) > 9, 'Error: ' || toString(max(blockSize())), 'Ok'), count() as total_rows, sum(ignore(*)) as dummy
FROM ( SELECT intDiv(number, 3) AS c0, 'a' || toString(number) as payload FROM numbers(10) ) AS t1
JOIN ( SELECT if(number == 0, 1000, 2) AS c0, 'b' || toString(number) as payload FROM numbers(10_000) ) t2
ON t1.c0 = t2.c0
SETTINGS max_joined_block_size_rows = 9;

SELECT
    if(max(blockSize()) > 1, 'Error: ' || toString(max(blockSize())), 'Ok'), count(), sum(sipHash64(t1.*))
FROM ( SELECT 1 AS c0, 'a' || toString(number) as payload FROM numbers(10) ) AS t1
JOIN ( SELECT 1 AS c0, 'b' || toString(number) as payload FROM numbers(10) ) t2
ON t1.c0 = t2.c0
SETTINGS max_joined_block_size_rows = 1;

SELECT
    if(max(blockSize()) > 1, 'Error: ' || toString(max(blockSize())), 'Ok'), count(), sum(sipHash64(t2.*))
FROM ( SELECT 1 AS c0, 'a' || toString(number) as payload FROM numbers(10) ) AS t1
JOIN ( SELECT 1 AS c0, 'b' || toString(number) as payload FROM numbers(10) ) t2
ON t1.c0 = t2.c0
SETTINGS max_joined_block_size_rows = 1;

{% for join_kind in ['INNER', 'LEFT', 'RIGHT', 'FULL'] -%}

SET max_block_size = {{ range(1, 10) | random }};

SELECT
    if(max(blockSize()) > 1, 'Error: ' || toString(max(blockSize())), 'Ok'), count() as total_rows, sum(sipHash64(*)) as dummy
FROM ( SELECT intDiv(number, 3) AS c0, 'a' || toString(number) as payload FROM numbers(10) ) AS t1
{{ join_kind }} JOIN ( SELECT if(number == 0, 1000, 2) AS c0, 'b' || toString(number) as payload FROM numbers(10) ) t2
ON t1.c0 = t2.c0
SETTINGS max_joined_block_size_rows = 1;

{% endfor -%}
